package com.example.lgseckill.controller;

import com.example.lgseckill.commom.Constant;
import com.example.lgseckill.commom.utils.RedisUtil;
import com.example.lgseckill.domain.SeckillOrder;
import com.example.lgseckill.domain.SeckillProductPreheatVO;
import com.example.lgseckill.domain.UserSeckillOrderInfoVO;
import com.example.lgseckill.service.ISeckillOrderService;
import com.example.lgseckill.service.ISeckillProducttService;
import com.example.lgseckill.service.impl.SeckillProductServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author:e
 * Date:2021/8/21 7:56 下午
 **/
@Controller
public class PayContorller {

    private final static Logger logger = LoggerFactory.getLogger(PayContorller.class);

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ISeckillOrderService seckillOrderService;

    @Autowired
    private ISeckillProducttService seckillProducttService;

    @RequestMapping("/pay")
    public String index(ModelMap map, Long seckillOrderId) {
        Date now = new Date();
        SeckillOrder seckillOrder = seckillOrderService.getById(seckillOrderId);
        int payStatus = seckillOrder.getPayStatus();
        map.addAttribute("seckillOrderId", seckillOrderId);
        map.addAttribute("userId", seckillOrder.getUserId());
        switch (payStatus) {
            case 1:
                logger.info("订单 {} 已经支付:", seckillOrderId);
                map.addAttribute("msg", "当前订单已经支付");
                break;
            case 2:
                logger.info("订单 {} 支付超时:", seckillOrderId);
                map.addAttribute("msg", "当前订单支付超时");
                break;
            default:
                //减库存
                seckillProducttService.productRefresh(seckillOrder.getSeckillId());

                seckillOrder.setPayStatus(1);
                seckillOrder.setPayType("aliPay");
                seckillOrderService.updateById(seckillOrder);
                logger.info("订单 {} 支付成功:", seckillOrderId);
                map.addAttribute("msg", "支付成功");
                //更新缓存库存
                SeckillProductPreheatVO seckillProductPreheatVO = (SeckillProductPreheatVO) redisUtil.get(Constant.SECKILL_PRODUCT_INFO + seckillOrder.getSeckillId());
                if (seckillProductPreheatVO!=null){
                    long endTime = seckillProductPreheatVO.getEndTime().getTime();
                    seckillProductPreheatVO.setNumber(seckillProductPreheatVO.getNumber()-1);
                    long expire = (endTime - now.getTime()) / 1000;
                    redisUtil.set(
                            Constant.SECKILL_PRODUCT_INFO + seckillProductPreheatVO.getSeckillProductId(),
                            seckillProductPreheatVO,
                            expire);
                }
        }
        return "pay_status";
    }

}
